08. Truthy/Falsy

Truthy/Falsy

INSTRUCTOR NOTE:

Check out this comparison of True and False in JavaScript and Python

There are only six falsy values in JavaScript so it is easiest to memorize those:

  • false
  • 0 (zero)
  • "" (empty string)
  • null
  • undefined
  • NaN

You can learn more about some of the quirks of truthy and falsy here.


What is NaN? NaN, or Not a Number, is a value that turns up when we ask Javascript to do certain impossible things in arithmetic -- like divide zero by zero.

Try it in the console -- and see if you can find the other two special numeric values, Infinity and -Infinity.

One last note: at 1:25, Cameron mentions that undefined means "that a variable doesn't exist and the interpreter doesn't know what you're referring to." This isn't quite true. If you try to use a var that hasn't been defined, you get a ReferenceError. You will see undefined if you declare a variable but don't assign it any values. Like so:

var a;
a;

will result in undefined. But

b;

will result in a ReferenceError because b hasn't been declared yet.

Follow your instructors!

@cwpittman

+jameswilliams